package com.situ.parking;

import java.util.List;
import java.util.Scanner;


public class ParkMange {
	Scanner sc = new Scanner(System.in);
	ParkModel pm = new ParkModel(null, null);
	ParkDao pd = new ParkDao();
	RecordDao rd = new RecordDao();

	public void start() {
		while (true) {
			System.out.println("-------停车管理系统-------");
			System.out.println("1.停车");
			System.out.println("2.查询全部停车位状态");
			System.out.println("3.取车");
			System.out.println("4.查询全部的收费记录");
			System.out.println("5.退出系统");
			System.out.println("请输入选项：");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				parking();
				break;
			case 2:
				findAllState();
				break;
			case 3:
				taking();
				break;
			case 4:
				selectAllRecord();
				break;
			case 5:
				return;
			default:
				break;
			}
		}
	}

	/**
	 * 停车
	 */
	public void parking() {
		while (true) {
			// 如果停车位满了则直接返回
			System.out.println("----停车场管理----");
			if (pm.getCount() >= 5) {
				System.out.println("停车位已满！！！");
				return;
			}
			int[] num = outputFreeSpace(); // 输出空闲车位
			System.out.println("输入你要停入的车位:");
			int number = Integer.parseInt(sc.nextLine());
			// 判断用户输入的车位号是否空闲
			boolean exist = false;
			for (int i = 0; i < num.length; i++) {
				if (number == num[i]) {
					exist = true;
					break;
				}
			}
			if (!exist) {
				System.out.println("您的输入有误！！");
				continue;
			}

			System.out.println("请输入车牌号：");
			String plate = sc.nextLine();
			System.out.println("请输入车辆高度：");
			float height = Float.parseFloat(sc.nextLine());
			if (height >= 3.0) {
				System.out.println("该车辆高度过高，进不去！！");
				continue;
			}
			ParkModel pm1 = new ParkModel(plate, 1);
			pm1.setNumber(number);
			if (pd.update(pm1) > 0) {
				System.out.println("停车成功！！");

				// 增添一条记录
				addRecord(number, plate, 1);

				pm.setCount(pm.getCount() + 1);
			} else {
				System.out.println("停车失败！！");
			}
			System.out.println("还停车不(y)  进入停车管理(n):");
			if (sc.nextLine().equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	/**
	 * 停车子功能 -- 输出空车位
	 */
	public int[] outputFreeSpace() {
		List<ParkModel> list = pd.selectList(new ParkModel(null, null));
		if (list == null) {
			System.out.println("停车有误！未存储车位");
		}
		int[] num = new int[list.size()];
		for (int i = 0; i < list.size(); i++) {
			ParkModel pm0 = list.get(i);
			if (pm0.getState() == 0) {
				num[i] = pm0.getNumber();
				System.out.println("车位号:" + pm0.getNumber() + " 空闲");
			}
		}
		return num;
	}

	/**
	 * 停车子功能 -- 增加一条记录
	 * 
	 * @param number
	 * @param plate
	 * @param type
	 * @param time
	 */
	public void addRecord(int number, String plate, int type) {
		RecordModel rm = new RecordModel();
		rm.setNumber(number);
		rm.setPlate(plate);
		rm.setType(type);
		if (rd.insert(rm) != 0) {
			System.out.println("记录插入成功！");
		} else {
			System.out.println("记录插入失败！");
		}
	}

	/**
	 * 查询全部停车位状态
	 */
	public void findAllState() {
		List<ParkModel> list = pd.selectList(new ParkModel(null, null));
		if (list == null) {
			System.out.println("未存储车位");
		}
		for (int i = 0; i < list.size(); i++) {
			ParkModel pm0 = list.get(i);
			String state = recodeState(pm0.getState());
			System.out.println("车位号:" + pm0.getNumber() + " 车位状态:" + state);
		}
	}

	/**
	 * 查询全部停车位状态子功能 -- 解码停车位状态
	 * 
	 * @param type
	 * @return 1 = 有车 0 = 空闲
	 */
	public String recodeState(int state) {
		return state == 1 ? "有车" : "空闲";
	}

	/**
	 * 还车
	 */
	public void taking() {
		System.out.println("------取车------");
		System.out.println("请输入车位号:");
		int number = Integer.parseInt(sc.nextLine());
		System.out.println("请输入车牌号:");
		String plate = sc.nextLine();
		ParkModel pm1 = findCar(number, plate);
		if (pm1 == null) {
			System.out.println("未找到车辆！！！");
			return;
		} else {
			System.out.println("请输入实际停车时间:");
			int time = Integer.parseInt(sc.nextLine());
			// 修改车位状态
			pm1.setState(0);
			pm1.setPlate(null);
			pm1.setCount(pm1.getCount() - 1);
			if (pd.update(pm1) != 0) {
				// 增加一条取车记录
				addQuRecord(number, plate, 0, time);
				System.out.println("取车成功！！");
			}
		}
	}

	/**
	 * 还车子功能 -- 查找停车位上的车辆
	 * 
	 * @param number
	 * @param plate
	 * @return
	 */
	public ParkModel findCar(int number, String plate) {
		ParkModel pm1 = new ParkModel(null, null);
		pm1 = pd.selectCar(number, plate);
		if (pm1.getState() == null) {
			System.out.println("该停车位没有该车辆！！");
			return null;
		}
		return pm1;
	}

	/**
	 * 还车子功能 -- 插入一条还车记录
	 * 
	 * @param number
	 * @param plate
	 * @param type
	 * @param time
	 */
	public void addQuRecord(int number, String plate, int type, int time) {
		RecordModel rm = new RecordModel();
		rm.setNumber(number);
		rm.setPlate(plate);
		rm.setType(type);
		rm.setFee(time * 2);
		rm.setTime(time);
		if (rd.insert(rm) != 0) {
			System.out.println("记录插入成功！");
			System.out.println("你需要缴纳的金额为: " + time * 2);

		} else {
			System.out.println("记录插入失败！");
		}
	}

	public void selectAllRecord() {
		List<RecordModel> list = rd.selectList();
		if (list == null) {
			System.out.println("未找到记录！！	");
		}
		for (RecordModel rm2 : list) {
			if (rm2.getType() == 0) {
				System.out.println("车位:" + rm2.getNumber() + " 车牌:" + rm2.getPlate() + " 费用:" + rm2.getFee() + " 停车时间:"
						+ rm2.getTime());
			}
		}
	}
}
